package com.neusoft.nep.PublicSupervisor.controller;
import com.neusoft.nep.common.FileUtils;
import com.neusoft.nep.entity.Feedback;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import java.io.IOException;
import java.net.URL;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SelectgridController {
    @FXML
    private ComboBox<String> cmbProvince, cmbCity;
    @FXML
    private TextField txtDistrict;
    @FXML
    private Button nextButton;
    private Map<String, List<String>> provinceMap = new HashMap<>();

    // 用于跨页面传递数据的静态变量
    public static String currentProvince;
    public static String currentCity;
    public static String currentAddress;

    @FXML
    public void initialize() {
        provinceMap.put("辽宁省",List.of("沈阳市","大连市"));
        provinceMap.put("吉林省",List.of("长春市","吉林市"));
        provinceMap.put("黑龙江省",List.of("哈尔滨市","齐齐哈尔市"));

        cmbProvince.setItems(FXCollections.observableArrayList(provinceMap.keySet()));
        cmbProvince.getSelectionModel().selectedItemProperty().addListener((ChangeListener<String>) (observable, oldValue, newValue) -> {
            List<String> cities = provinceMap.getOrDefault(newValue, List.of());
            cmbCity.setItems(FXCollections.observableArrayList(cities));
            cmbCity.getSelectionModel().clearSelection();
        });
    }

    public void next() throws IOException {
        String province = cmbProvince.getValue();
        String city = cmbCity.getValue();
        String address = txtDistrict.getText();

        if (province == null || city == null || address.isEmpty()) {
            // 提示必填项
            return;
        }

        // 保存当前选择的网格信息
        currentProvince = province;
        currentCity = city;
        currentAddress = address;

        // 跳转到AQI选择页面
        Stage stage = (Stage) nextButton.getScene().getWindow();
        FXMLLoader fxmlLoader = new FXMLLoader();
        URL url = getClass().getResource("../View/Selectaqi.fxml");
        fxmlLoader.setLocation(url);
        Parent root = fxmlLoader.load();
        Scene scene = nextButton.getScene();
        scene.setRoot(root);
        stage.setScene(scene);
        stage.show();
    }
}